<?php

/* Tangozebra Web Site CMS Insert New Sector
 * (c)2003 Digital Advertising and Marketing Ltd
 */

require("../session.inc");
require("../include.inc");
require("../functions.inc");
require_once("verify_login.phtml"); //check if the user is logged in, include this one every access restricted page

$xml_url = $_SERVER['PHP_SELF'];
$xml_url .= "?xslt=no";
$header = "Compose, test and send newsletter";

//////////////////////////////////////////////////////////////////////
// DB connection
//////////////////////////////////////////////////////////////////////

$db = mysql_connect($dbhost,$dbuser,$dbpass)
     or die("Could not connect to DB!");

mysql_select_db($dbname,$db)
     or die("Could not select DB!");

//////////////////////////////////////////////////////////////////////
// Get the data for the content screen
//////////////////////////////////////////////////////////////////////

$sql_sector = "SELECT * FROM sector where main='1' ORDER BY sectororder";
$result_sector = mysql_query($sql_sector)
                 or die ("Could not retrive data from DB");

///////////////////////////////////////////////////////////////////////
// Write XML document
///////////////////////////////////////////////////////////////////////

function build_subs(&$elt, $id) {
  $query = "select * from sector where sub_to='$id' ORDER BY sectororder";
  $result = mysql_query($query);
  while ($row = mysql_fetch_array($result)) {
    $sub_elt = $elt->new_child("sub");
    $sub_elt->set_attribute("id", $row['id']);   
    $sub_elt->set_attribute("name", $row['name']);
    $sub_elt->set_attribute("order", $row['sectororder']);
    build_subs($sub_elt, $row['id']);
  }
}

$doc = domxml_new_xmldoc("1.0");
cms_navigation($doc, $IDtag, $xml_url, $header);

if ($confirmation=='true'){
  $wp->set_attribute("confirmation", "true");
}

$sc = $wp->new_child("sector");

// build sector tree
while ($value = mysql_fetch_array($result_sector)) {
  $mn = $sc->new_child("main","");
  $mn->set_attribute("id", $value['id']); 
  $mn->set_attribute("name", $value['name']);
  $mn->set_attribute("order", $value['sectororder']);
  build_subs($mn, $value['id']);
}

//////////////////////////////////////////////////////////////////////
// XSLT process
//////////////////////////////////////////////////////////////////////

$xsltdoc = "../../xsl/cms/cms_newsletter.xsl";
htmlout($doc, $xsltdoc, $xslt);
exit;

?>